<?php
namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;

class KouController extends Controller
{
      public function index(){
              $fen = DB::table('fenlei')->get();
         $data = DB::table('kouling')->select('kouling.*','fenlei.name')->leftJoin('fenlei', 'kouling.type_id', '=', 'fenlei.id')->paginate(15);
         return view('admin.kouling.index',compact('data','fen'));
      }

      public function create(){
        $data = DB::table('fenlei')->get();
         return view('admin.kouling.add',compact('data'));
      }

      //编辑视图
      public function edit($id){

        $data = DB::table('fenlei')->get();
        $kid = $id;
        return view('admin.kouling.edit',compact('data','kid'));
      }



//更新提交
    public function update(Request $request,$id){

      $res = DB::table('kouling')->where('id',$id)->update([
            'content' => $request->content,
            'type_id' => $request->type_id,
        ]);
      if ($res == 1) {
         return redirect('admin/kou');
      }else{
        return back()->with('更新失败！');
      }
    }

//搜索提交
    public function store(Request $request){
          $type_id = $request->type_id;
          $content = $request->content;
          $arr = [
             'type_id' => $type_id,
             'content' => $content,
             'created_at' => time()
          ];
          DB::table('kouling')->insert($arr);
          return redirect('admin/kou');
    }

     //删除
      public function destroy($id)
    {

      $post = DB::table('kouling')->where('id', $id)->delete();
      return  $data = ['status' => 0,'msg' => '删除成功！'];
    }


}
